open Fr
open FrGui

(* TODO: contemplate share
   smart guessing of alpha setting
    (wrong setting causes Gpointer.Null b/c no display available) *)
let gl_area ?(alpha=true) ?(doublebuffer=true) ?menu ?popup f =
	let options = List.fold_left (fun l (k, v) -> if v then k :: l else l) []
		[ `RGBA, alpha;
		  `DOUBLEBUFFER, doublebuffer ] in
	let widget = GlGtk.area options ()
	and realized = ref false in
	ignore (widget#connect#realize (fun () -> realized := true));
	let display = lift_signal widget#connect#display
	and redraw f =
		if !realized then begin
			widget#make_current ();
			f ();
			if doublebuffer then widget#swap_buffers ()
		end;
	in
	register_e_with widget (map_e redraw (snapshot_b f display));
	add_popup_menu ?menu ?popup widget;
	coerce_widget (new behavior_view_widget
		~expand: true
		widget redraw f)
